package team.first.dataimport;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.neo4j.driver.Session;
import team.first.util.DriverUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first
 * @Date: 2021/12/3 11:16
 * @Description:
 */
public class MovieCastImport {
    public static void movieCastImport() {
        Session session = DriverUtil.getNewSession();
        try {
            List<Entity> movieCastList = Db.use().query("select * from movie_cast");
            String actedRel = "MATCH (m:Movie{id:$movieId})" +
                    "MATCH (s:Staff{id:$staffId})" +
                    "CREATE (s) - [:ACTED{id:$id, actAs:$actAs, roleTypeId:1}] -> (m) ";
            String directedRel = "MATCH (m:Movie{id:$movieId})" +
                    "MATCH (s:Staff{id:$staffId})" +
                    "CREATE (s) - [:DIRECTED{id:$id, actAs:'导演', roleTypeId:2}] -> (m) ";

            movieCastList.forEach(movieCast -> {
                Map<String, Object> params = new HashMap<>();
                params.put("id", movieCast.getInt("id"));
                params.put("movieId", movieCast.getInt("movie_id"));
                params.put("staffId", movieCast.getInt("staff_id"));
                params.put("actAs", movieCast.getStr("act_as"));
                if (movieCast.getInt("role_type_id") == 1) {
                    session.run(actedRel, params);
                } else {
                    session.run(directedRel, params);
                }
            });
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DriverUtil.close(session);
        }

    }
}
